package com.xoa.service.WFE.WFELog;


import com.xoa.dao.workflow.FlowManageLogMapper;
import com.xoa.dao.workflow.FlowRunLogMapper;
import com.xoa.model.users.Users;
import com.xoa.model.workflow.FlowManageLog;
import com.xoa.model.workflow.FlowRunLog;
import com.xoa.util.CookiesUtil;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.ipUtil.IpAddr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.misc.LRUCache;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

@Service
public class WFELog {

        @Autowired
        private FlowRunLogMapper flowRunLogMapper;

        @Autowired
        private FlowManageLogMapper flowManageLogMapper;

        private final String left="[";

        private final String right="]";

        private int count;//影响行数

        /**
         * @创建作者:李然  Lr
         * @方法描述：添加工作流程使用日志
         * @创建时间：11:25 2018/7/20
         **/
        public int addFlowRunLog(FlowRunLog flowRunLog){
                //传入的对象和流程实例Id--runId不能为空
                if(flowRunLog!=null && flowRunLog.getRunId()!=null){
                        //通过runId来获取日志所需要的runName和flowId，并填充到flowRunLog对象中
                        FlowRunLog flowRunLogDB=flowRunLogMapper.selectFlowRunLogByRunId(flowRunLog.getRunId());
                        flowRunLog.setRunName(flowRunLogDB.getRunName());
                        flowRunLog.setFlowId(flowRunLogDB.getFlowId());
                        //根据flowId和prcsId获得步骤（第几步）名字
                        if(flowRunLog.getPrcsId()!=null){
                                flowRunLog.setPrcsName(flowRunLogMapper.selectPrcsName(flowRunLog.getFlowPrcs(),flowRunLog.getFlowId()));
                        }
                        if(!StringUtils.checkNull(flowRunLog.getGoProcsId())){
                                flowRunLog.setGoPrcsName(flowRunLogMapper.selectPrcsName(Integer.parseInt(flowRunLog.getGoProcsId()),flowRunLog.getFlowId()));
                        }
                        //根据flowRunLog中的type装配日志信息content
                        flowRunLog.setContent(getFlowRunLogContent(flowRunLog));
                        //执行日志插入功能并返回操作行数
                        count=flowRunLogMapper.insertSelective(flowRunLog);
                }
                return count;
        }

        private String getFlowRunLogContent(FlowRunLog flowRunLog){
                String content=null;//日志信息
                switch (flowRunLog.getType()){
                        //转交
                        case "1":content=left+flowRunLog.getUser().getUserName()+right+"将流水号为"+
                                left+flowRunLog.getRunId()+right+"的流程从"+left+"第"+flowRunLog.getFlowPrcs()+"步骤："+flowRunLog.getPrcsName()+right
                                +"转交至"+left+"第"+flowRunLog.getGoProcsId()+"步骤："+flowRunLog.getGoPrcsName()+right;
                                break;
                        //保存
                        case "2":content=left+flowRunLog.getUser().getUserName()+right+"将流水号为"+
                                left+flowRunLog.getRunId()+right+"的流程"+left+"在第"+flowRunLog.getFlowPrcs()+"步骤："+flowRunLog.getPrcsName()+right
                                +"进行保存";
                                break;
                        //回退
                        case "3":content=left+flowRunLog.getUser().getUserName()+right+"将流水号为"+
                                left+flowRunLog.getRunId()+right+"的流程从"+left+"第"+flowRunLog.getFlowPrcs()+"步骤："+flowRunLog.getPrcsName()+right
                                +"回退至"+left+"第"+flowRunLog.getGoProcsId()+"步骤："+flowRunLog.getGoPrcsName()+right;
                                break;
                        //收回
                        case "4":content=left+flowRunLog.getUser().getUserName()+right+"将流水号为"+
                                left+flowRunLog.getRunId()+right+"的流程"+left+"在第"+flowRunLog.getFlowPrcs()+"步骤："+flowRunLog.getPrcsName()+right
                                +"进行收回操作";
                                break;
                        //归档
                        case "5":content=left+flowRunLog.getUser().getUserName()+right+"将流水号为"+
                                left+flowRunLog.getRunId()+right+"的流程进行归档操作";
                                break;
                        //导出
                        case "6":content=left+flowRunLog.getUser().getUserName()+right+"将流水号为"+
                                left+flowRunLog.getRunId()+right+"的流程进行导出操作";
                                break;
                        //打印
                        case "7":content=left+flowRunLog.getUser().getUserName()+right+"将流水号为"+
                                left+flowRunLog.getRunId()+right+"的流程进行打印操作";
                                break;
                        //上传附件
                        case "8":content=left+flowRunLog.getUser().getUserName()+right+"将流水号为"+
                                left+flowRunLog.getRunId()+right+"的流程从"+left+"在第"+flowRunLog.getFlowPrcs()+"步骤："+flowRunLog.getPrcsName()+right
                                +"上传了附件，附件名称为："+left+flowRunLog.getAccessory()+right;
                                break;
                        //工作委托
                        case "9":content=left+flowRunLog.getUser().getUserName()+right+"将流水号为"+
                                left+flowRunLog.getRunId()+right+"的流程工作委托给："+left+flowRunLog.getTransactor()+right;
                                break;
                        //删除工作
                        case "10":content=left+flowRunLog.getUser().getUserName()+right+"将流水号为"+
                                left+flowRunLog.getRunId()+right+"的流程进行删除操作";
                                break;
                        //结束工作
                        case "11":content=left+flowRunLog.getUser().getUserName()+right+"将流水号为"+
                                left+flowRunLog.getRunId()+right+"的流程进行结束流程操作";
                                break;
                        //强制结束
                        case "12":content=left+flowRunLog.getUser().getUserName()+right+"将流水号为"+
                                left+flowRunLog.getRunId()+right+"的流程进行强制结束操作";
                                break;
                        //销毁工作
                        case "13":content=left+flowRunLog.getUser().getUserName()+right+"将流水号为"+
                                left+flowRunLog.getRunId()+right+"的流程进行销毁操作";
                                break;
                        //还原工作
                        case "14":content=left+flowRunLog.getUser().getUserName()+right+"将流水号为"+
                                left+flowRunLog.getRunId()+right+"的流程进行还原操作";
                                break;
                        //编辑工作
                        case "15":content=left+flowRunLog.getUser().getUserName()+right+"将流水号为"+
                                left+flowRunLog.getRunId()+right+"的流程"+left+"在第"+flowRunLog.getFlowPrcs()+"步骤："+flowRunLog.getPrcsName()+right
                                +"进行编辑操作";
                                break;
                        //催办工作
                        case "16":content=left+flowRunLog.getUser().getUserName()+right+"催办"+left+flowRunLog.getTransactor()+right+"尽快进行工作办理";
                                break;
                        //在我的工作中删除待办工作
                        case "17":content=left+flowRunLog.getUser().getUserName()+right+"在我的工作中将流水号为"+
                                left+flowRunLog.getRunId()+right+"的流程进行删除操作";
                                break;
                        //工作交办
                        case "18":content=left+flowRunLog.getUser().getUserName()+right+"将流水号为"+
                                left+flowRunLog.getRunId()+right+"的流程在第"+flowRunLog.getFlowPrcs()+"步骤："+"交办给"+flowRunLog.getTransactor();
                                break;
                        //办理完毕
                        case "19":content=left+flowRunLog.getUser().getUserName()+right+"将流水号为"+
                                left+flowRunLog.getRunId()+right+"的流程办理完毕";
                                break;
                        //增加经办人
                        case "20":content=left+flowRunLog.getUser().getUserName()+right+"将流水号为"+
                                left+flowRunLog.getRunId()+right+"的流程在第"+flowRunLog.getFlowPrcs()+"步骤："+"增加经办人给"+flowRunLog.getTransactor();
                                break;

                }


                return content;
        }

        /**
         * @创建作者:李然  Lr
         * @方法描述：根据request自动装配User，ip.并返回flowRunLog对象
         * @创建时间：13:12 2018/7/20

         **/
        public FlowRunLog getFlowRunLogByRequest(HttpServletRequest request){
                FlowRunLog flowRunLog=new FlowRunLog();
                Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
                Users user = SessionUtils.getSessionInfo(request.getSession(),Users.class,new Users(),redisSessionId);
                String ip=IpAddr.getIpAddr(request);
                flowRunLog.setUser(user);
                flowRunLog.setIp(ip);
                return  flowRunLog;
        }
        /**
         * @创建作者:李然  Lr
         * @方法描述：根据request自动装配User，ip.并返回flowManageLog对象
         * @创建时间：14:29 2018/7/20

         **/
        public FlowManageLog getFlowManageLog(HttpServletRequest request){
                FlowManageLog flowManageLog=new FlowManageLog();
                Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
                Users user = SessionUtils.getSessionInfo(request.getSession(),Users.class,new Users(),redisSessionId);
                String ip=IpAddr.getIpAddr(request);
                flowManageLog.setUser(user);
                flowManageLog.setIp(ip);
                return flowManageLog;
        }

        /**
         * @创建作者:李然  Lr
         * @方法描述：添加工作流程设计日志
         * @创建时间：13:57 2018/7/20
         **/
        public int addFlowManageLog(FlowManageLog flowManageLog){
                //传入的对象和flowId不能为空
                if(flowManageLog!=null && flowManageLog.getFlowId()!=null){
                        //根据flowId查找对应的flowName并装配进去
                        if(flowManageLog.getFlowName()==null || flowManageLog.getFlowName().length()==0) {
                                flowManageLog.setFlowName(flowManageLogMapper.selectFlowName(flowManageLog.getFlowId()));
                        }
                        flowManageLog.setContent(getFlowManageLogContent(flowManageLog));
                        //插入日志并返回影响行数
                        count=flowManageLogMapper.insertSelective(flowManageLog);
                }
                return count;
        }


        private String getFlowManageLogContent(FlowManageLog flowManageLog){
                String content=null;//日志信息
                switch (flowManageLog.getType()){
                        //新建表单
                        case 1:content=left+flowManageLog.getUser().getUserName()+right+"新建表单，表单名称："+
                                left+flowManageLog.getTableName()+right;
                                break;
                        //保存表单
                        case 2:content=left+flowManageLog.getUser().getUserName()+right+"保存表单，表单名称："+
                                left+flowManageLog.getTableName()+right;
                                break;
                        //新建流程
                        case 3:content=left+flowManageLog.getUser().getUserName()+right+"新建流程，流程名称："+
                                left+flowManageLog.getFlowName()+right;
                                break;
                        //新建步骤
                        case 4:content=left+flowManageLog.getUser().getUserName()+right+"在"+left+flowManageLog.getFlowId()+right
                                +"这个流程中新建步骤，步骤名称："+left+flowManageLog.getPrcsName()+right;
                                break;
                        //可写字段
                        case 5:content=left+flowManageLog.getUser().getUserName()+right+"设置流程步骤："+
                                left+flowManageLog.getPrcsName()+right+"的可写字段";
                                break;
                        //保密字段
                        case 6:content=left+flowManageLog.getUser().getUserName()+right+"设置流程步骤："+
                                left+flowManageLog.getPrcsName()+right+"的保密字段";
                                break;
                        //必填字段
                        case 7:content=left+flowManageLog.getUser().getUserName()+right+"设置流程步骤："+
                                left+flowManageLog.getPrcsName()+right+"的必填字段";
                                break;
                        //经办权限
                        case 8:content=left+flowManageLog.getUser().getUserName()+right+"更新流程步骤："+
                                left+flowManageLog.getPrcsName()+right+"的经办权限";
                                break;
                        //智能选人
                        case 9:content=left+flowManageLog.getUser().getUserName()+right+"更新流程步骤："+
                                left+flowManageLog.getPrcsName()+right+"的智能选人";
                                break;
                        //流转设置
                        case 10:content=left+flowManageLog.getUser().getUserName()+right+"更新流程步骤："+
                                left+flowManageLog.getPrcsName()+right+"的流转设置";
                                break;
                        //编辑流程步骤
                        case 11:content=left+flowManageLog.getUser().getUserName()+right+"编辑流程步骤："+
                                left+flowManageLog.getPrcsName()+right+"的基本设置";
                                break;
                        //删除流程
                        case 12:content=left+flowManageLog.getUser().getUserName()+right+"删除流程："
                                +left+flowManageLog.getFlowName()+right;
                                break;
                        //删除步骤
                        case 13:content=left+flowManageLog.getUser().getUserName()+right+"删除"+
                                left+flowManageLog.getFlowId()+right+"流程的步骤："+left+flowManageLog.getPrcsName()+right;
                                break;
                        //修改权限
                        case 14:content=left+flowManageLog.getUser().getUserName()+right+"变更权限："
                                +left+flowManageLog.getAuthorityName()+right;
                                break;
                        //删除权限
                        case 15:content=left+flowManageLog.getUser().getUserName()+right+"变更权限："
                                +left+flowManageLog.getAuthorityName()+right;
                                break;
                        //导入流程
                        case 16:content=left+flowManageLog.getUser().getUserName()+right+"导入流程："
                                +left+flowManageLog.getFlowName()+right;
                                break;
                        //清空流程
                        case 17:content=left+flowManageLog.getUser().getUserName()+right+"清空流程："
                                +left+flowManageLog.getFlowName()+right;
                                break;
                        //编辑表单
                        case 18:content=left+flowManageLog.getUser().getUserName()+right+"编辑表单，表单名称："
                                +left+flowManageLog.getAuthorityName()+right;
                                break;
                        //导入表单
                        case 19:content=left+flowManageLog.getUser().getUserName()+right+"导入表单，表单名称："
                                +left+flowManageLog.getAuthorityName()+right;
                                break;
                        //删除表单
                        case 20:content=left+flowManageLog.getUser().getUserName()+right+"删除表单，表单名称："
                                +left+flowManageLog.getAuthorityName()+right;
                                break;
                        //编辑流程
                        case 21:content=left+flowManageLog.getUser().getUserName()+right+"编辑流程："
                                +left+flowManageLog.getFlowName()+right;
                                break;
                        //删除连线
                        case 22:content=left+flowManageLog.getUser().getUserName()+right+"删除连线："
                                +left+flowManageLog.getPrcsName()+right;
                                break;
                        //节点连线
                        case 23:content=left+flowManageLog.getUser().getUserName()+right+"节点连线："
                                +left+flowManageLog.getPrcsName()+","+right;
                                break;
                }
                return content;
        }

        /**
         *
         * @param request
         * @param runId  流水号
         * @param flowId  流程ID
         * @param prcsId   流程实例步骤号
         * @param flowPrcs  流程设计步骤号
         * @param goPrcsId  转交到的流程设计步骤号
         * @param type    判断日志类型
         * @return
         */
        public int getFlowRunLogByRequest(HttpServletRequest request,Integer runId,
                                                 Integer flowId,Integer prcsId,Integer flowPrcs,
                                          String goPrcsId,String type){
                FlowRunLog flowRunLog=new FlowRunLog();
                Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
                Users user = SessionUtils.getSessionInfo(request.getSession(),Users.class,new Users(),redisSessionId);
                String ip=IpAddr.getIpAddr(request);
                flowRunLog.setUser(user);
                flowRunLog.setIp(ip);
                flowRunLog.setRunId(runId);
                flowRunLog.setFlowId(flowId);
                flowRunLog.setFlowPrcs(flowPrcs);
                flowRunLog.setPrcsId(prcsId);
                flowRunLog.setGoProcsId(goPrcsId);
                flowRunLog.setType(type);
                return addFlowRunLog(flowRunLog);
        }

}

